﻿using AnlianCommon.Common.Domains.BankCustomers;
using AnlianCommon.Common.DTO;
using AnlianCommon.Common.Infrastructure.IRepository;
using AnlianCommon.Enum;
using AutoMapper;
using MediatR;

namespace SheepCommon.API.Procurements
{
    public class ProcurementCreateHandler : IRequestHandler<ProcurementCreateCommand, Result>
    {
        private readonly IBaseRepository<GPprice> _gPpriceRepository;
        private readonly IMapper _mapper;

        public ProcurementCreateHandler(IBaseRepository<GPprice> gPpriceRepository, IMapper mapper)
        {
            _gPpriceRepository = gPpriceRepository;
            _mapper = mapper;
        }
        /// <summary>
        /// 采购新增
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result> Handle(ProcurementCreateCommand request, CancellationToken cancellationToken)
        {
            var result = new Result();
            try
            {
                GPprice gPprice = _mapper.Map<GPprice>(request);
                var res  =await _gPpriceRepository.Add(gPprice);
                if (res > 0)
                {
                    result.Code  = (int)CommonErrorCode.Success;
                    result.Msg = "添加成功";
                }
                else
                {
                    result.Msg = "";
                }
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
    }
}
